package com.hspedu.arrays;

import java.util.Arrays;
import java.util.Comparator;
//数组定制排序
public class ArraysExercise {
    public static void main(String[] args) {
        /*
        案例：自定义Book类，里面包含name和price，按price排序(从大到小)。
        要求使用两种方式排序 , 有一个 Book[] books = 4本书对象.

        使用前面学习过的传递 实现Comparator接口匿名内部类，也称为定制排序。
        [同学们完成这个即可 10min  ],
        可以按照 price (1)从大到小 (2)从小到大 (3) 按照书名长度从大到小

         */
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦", 100);
        books[1] = new Book("金瓶梅新", 90);
        books[2] = new Book("青年文摘20年", 5);
        books[3] = new Book("java从入门到放弃~", 300);


        //Book.sortByPrice1(books);

        //Book.sortByPrice2(books);

        Book.sortByBookName(books);



        System.out.println(Arrays.toString(books));

    }
}


class Book{
    private String name;
    private double price;

    public Book() {
    }

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }


    //按照图书的价格从小到大进行排序
    public static void sortByPrice1(Book[] books){
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                double num = b1.getPrice() - b2.getPrice();
                if (num > 0){
                    return 1;
                }else if(num < 0){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
    }

    //按照图书的价格从大到小进行排序
    public static void sortByPrice2(Book[] books){
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                double num = b1.getPrice() - b2.getPrice();
                if (num > 0){
                    return -1;
                }else if(num < 0){
                    return 1;
                }else{
                    return 0;
                }
            }
        });
    }

    //按照图书的名称从小到大进行排序
    public static void sortByBookName(Book[] books){
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                double num = b1.getName().length() - b2.getName().length();
                if (num > 0){
                    return 1;
                }else if(num < 0){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
    }
}